<?php

namespace Yolo\Components\EventDispatcher;

use Yolo\ComponentManager\ComponentManager;
use Yolo\Observers\ObserverInterface;
use Yolo\Service\Subjects\AfterWorkerStartedSubject;

class AfterWorkerStartedObserver implements ObserverInterface
{
    public function __construct(
        private readonly ComponentManager $componentManager,
    ){}

    /**
     * The list of events this observer is listening to.
     * @return string[]
     */
    public function listens(): array
    {
        return [
            AfterWorkerStartedSubject::class,
        ];
    }

    public function process(string $event, array $data): void
    {
        $eventDispatcherComponent = $this->componentManager->getRegisteredComponent(EventDispatcherComponent::class);
        if (!$eventDispatcherComponent) {
            return;
        }

        // 放到后台去跑，防止阻塞
        \Hyperf\Coroutine\go(function () use ($eventDispatcherComponent) {
            $eventDispatcherComponent->listens();
        });
    }
}